﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using My.DataAccess.Core;
using NCLCustomerManagement.DataAccess.User;
using NCLCustomerManagement.DataTransferObjects.User;

namespace NCLCustomerManagement.BusinessLogic.User
{
    public class UserRequestBC : BaseBC, IUserRequestBC
    {
        private IUserRequestRepository _requestRepo;

        public UserRequestBC(IUserRequestRepository requestRepo)
        {
            _requestRepo = requestRepo;

        }

        public IEnumerable<UserRequestDTO> GetApprovedOrRejectedSentRequests(string user)
        {
            return _requestRepo.GetSentRequests(user)
                .Where(e => e.Status != RequestStatus.Initial)
                .ToList();
        }


        public IEnumerable<UserRequestDTO> GetReceivedRequests(string user)
        {
            return _requestRepo.GetReceivedRequests(user);
        }

        public override IBaseRepository<DTO> GetRepository<DTO>()
        {
            return _requestRepo as IBaseRepository<DTO>;
        }


        public void UpdateRequestAsViewed(int id)
        {
            _requestRepo.UpdateRequestAsViewed(id);
        }


        public int GetUnviewedMessageCount(string user, bool isAdmin)
        {
            int count;
            if (isAdmin)
                count = GetReceivedRequests(user)
                    .Where(e => e.Status == RequestStatus.Initial)
                    .Count();
            else
                count = GetApprovedOrRejectedSentRequests(user)
                    .Where(e => !e.Viewed)
                    .Count();
            return count;
        }


        public void DeleteRequest(int id)
        {
            _requestRepo.Delete(id);
        }


        public void ApproveOrRejectRequest(int id, bool approve)
        {
            if (approve)
                _requestRepo.Approve(id);
            else
                _requestRepo.Reject(id);
          
        }


        public UserRequestDTO GetRequest(int id)
        {
            return _requestRepo.GetRequest(id);
        }
    }
}
